from sqlalchemy import Column, Integer, String, DateTime, Date, Time, Boolean, ForeignKey
from databases import Base
from datetime import datetime
from sqlalchemy.orm import relationship

'''
本文件定义了数据表的参数
'''


# 定义Pause（短暂停）对象:
class Pause(Base):
    # 表的名字:
    __tablename__ = 'pause'
    # 表的结构:
    ID = Column(Integer, primary_key=True, autoincrement=True)
    line = Column(String(10), nullable=False)
    AlarmDate = Column(Date, nullable=False)
    AlarmTime = Column(Time, nullable=False)
    AlarmType = Column(String(12), nullable=False)
    VarName = Column(String(30), nullable=False)
    DateTime = Column(DateTime, nullable=False)
    isMatch = Column(Boolean)

    __mapper_args__ = {
        "order_by": DateTime.desc()
    }


class Analyse_Pause(Base):
    # 表的名字:
    __tablename__ = 'analyse_pause'
    # 表的结构:
    ID = Column(Integer, primary_key=True, autoincrement=True)
    StartLine = Column(String(10), nullable=False)
    StartVarName = Column(String(30), nullable=False)
    StartAlarm = Column(String(10), nullable=False)
    StartID = Column(Integer)
    StartTime = Column(DateTime, nullable=False)
    EndLine = Column(String(10), nullable=False)
    EndVarName = Column(String(30), nullable=False)
    EndAlarm = Column(String(10), nullable=False)
    EndID = Column(Integer)
    EndTime = Column(DateTime, nullable=False)
    Interval = Column(Integer, nullable=False)
    isActive = Column(Boolean)

    __mapper_args__ = {
        "order_by": EndTime.desc()
    }


class LineStatus(Base):
    __tablename__ = 'status'
    ID = Column(Integer, primary_key=True, autoincrement=True)
    Line = Column(String(10), nullable=False)
    Network = Column(Boolean, nullable=False)
    PLC = Column(Boolean, nullable=False)
    DB_interval = Column(Integer, nullable=False)
    Startby = Column(Integer, nullable=False)
    UpdateTime = Column(DateTime, nullable=False, default=datetime.now)


class Outputs(Base):
    __tablename__ = 'ouputs'
    ID = Column(Integer, primary_key=True, autoincrement=True)
    Line = Column(String(10), nullable=False)
    Plan_output = Column(Integer)
    Current_output = Column(Integer)
    UpdateTime = Column(DateTime, nullable=False, default=datetime.now)


class Users(Base):
    __tablename__ = 'users'
    User_ID = Column(Integer, primary_key=True, nullable=False)
    Factory = Column(String(20), nullable=False)
    Name = Column(String(20), nullable=False)
    Pinyin = Column(String(20), nullable=False)
    Email = Column(String(30), nullable=False)
    Tel = Column(String(20), nullable=False)
    isActive = Column(Boolean)


class Notice(Base):
    __tablename__ = 'notice'
    ID = Column(Integer, primary_key=True, autoincrement=True)
    Factory = Column(String(20), nullable=False)
    Notice_num = Column(String(20), nullable=False)
    Title = Column(String(50), nullable=False)
    Name = Column(String(20), nullable=False)
    Name_ID = Column(Integer, ForeignKey("users.User_ID"), nullable=False)
    UpdateTime = Column(DateTime, nullable=False, default=datetime.now)
    author = relationship("Users", backref="notices")


# class Contract(Base):
#     __tablename__ = 'contract'
#     ID = Column(Integer, primary_key=True, autoincrement=True)
#     Factory = Column(String(20), nullable=False)
#     Contract_num = Column(String(30), nullable=False)
#     Title = Column(String(50), nullable=False)
#     Name = Column(String(20), nullable=False)
#     Name_ID = Column(Integer, ForeignKey("users.User_ID"), nullable=False)
#     Company = Column(String(100), ForeignKey("company.Company"),nullable=False)
#     Price = Column(Integer, nullable=False)
#     UpdateTime = Column(DateTime, nullable=False, default=datetime.now)
#     author = relationship("Users", backref="contracts")


class Company(Base):
    __tablename__ = 'company'
    ID = Column(Integer, primary_key=True, autoincrement=True)
    Company_ID = Column(Integer)
    Company = Column(String(100), nullable=False)
    Addr = Column(String(200), nullable=False)
    Legal = Column(String(20), nullable=False)
